<?php
/**
 * Created by PhpStorm.
 * User: Kenny.Hu
 * Date: 2016/1/28
 * Time: 17:05
 */

//远程调用
function curl_post($url, $post_data = array()){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    if(! empty($post_data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

//PHP对象 转 数组 - 仅限一维数组转换
function obj2arr( $std, $include = array() ){
    $arr = array();
    foreach( $std as $k => $v ){
        if( !empty($include) ){
            if(! in_array($k, $include)) continue;
        }
        $arr[$k] = $v;
    }
    return $arr;
}


//中云通数据库初始化
function zyt_data_init(){
    import('Org.Net.Zyt');
    $db_config = C('ZYT_DB');
    return new \Zyt\Api($db_config);
}

//form_hash 生成器 - 主要用户防止重复提交订单
function form_hash(){
    $hash = rand(10000, 99999);
    session('form_hash', $hash);
    return $hash;
}

//比较form_hash
function compare_form_hash($hash){
    if( !empty($hash) && $hash == session('form_hash') ){
        session('form_hash', null);
        return true;
    }else{
        $Common = new \Common\Controller\CommonController();
        $Common->_error('该页面已失效');
    }
}

// 数据库money字段 772.9900， 转化为  772.99
function price($money, $return_string = true){
    if( is_null($money)  ) return '';
    $float = floatval($money);
    return false === $return_string ? number_format($float, 2, '.', '') : floatval(number_format($float, 2, '.', ''));
}

//分割游戏币名称与充值比例数据
function sliceGameRate($string){
    $result = array();
    $tmp = explode(',', $string);
    if(empty($tmp) && count($tmp) !== 2)  return $result;
    $result['currency'] = $tmp[0];
    $result['ratio']    = $tmp[1];
    $rate = 0;
    $tmp2 = explode(':', $tmp[1]);
    if( !empty($tmp2) && count($tmp2) === 2 ){
        $rate = floatval(floatval($tmp2[1]) / floatval($tmp2[0]));
        $result['tb']   = (int)$tmp2[0];
        $result['yxb']  = (int)$tmp2[1];
    }
    $result['rate'] = $rate;
    return $result;
}

function check_email($email){
    if (ereg('^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+',$email)){
        return true;
    }else{
        return false;
    }
}
function check_mobile($mobile){
    if (preg_match('/^1[34578]\d{9}$/', $mobile)) {
        return true;
    } else {
        return false;
    }
}
function check_username($name){
    if(preg_match('/^[a-zA-Z_\d]{4,20}$/', $name) && ! preg_match('/^\d{11}$/', $name)){
        return true;
    }else{
        return false;
    }
}
function check_userpass($pass){
    if(preg_match('/^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{6,20}$/', $pass)){
        return true;
    }else{
        return false;
    }
}
function str_to_num($str){
    $number = floatval($str);
    return number_format($number, 2);
}

/**
 * 将XML数据转换为数组
 *
 * @param $xml
 * @return mixed
 */
function xml_to_array($xml){
    $reg = '/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/';
    if(preg_match_all($reg, $xml, $matches)){
        $count = count($matches[0]);
        for($i = 0; $i < $count; $i++){
            $subxml= $matches[2][$i];
            $key = $matches[1][$i];
            if(preg_match( $reg, $subxml )){
                $arr[$key] = xml_to_array( $subxml );
            }else{
                $arr[$key] = $subxml;
            }
        }
    }
    return $arr;
}

//获取当前完整网址
function get_current_url(){
    $s = $_SERVER;
    return base64_encode($s['REQUEST_SCHEME'] . '://' . $s['SERVER_NAME'] . $s['REQUEST_URI']);
}

// 获取返回网址
function get_ref_url(){
    $ref = I('get.ref');
    return empty($ref) ? '' : base64_decode($ref);
}

//去掉字符串最后一个特定的字符
function extraCharFilter($string, $char = ','){
    return trim($string, $char);
}

//截取字符串
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true) {
    if(function_exists("mb_substr"))
        $slice = mb_substr($str, $start, $length, $charset);
    elseif(function_exists('iconv_substr')) {
        $slice = iconv_substr($str,$start,$length,$charset);
        if(false === $slice) {
            $slice = '';
        }
    }else{
        $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
        $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
        $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
        preg_match_all($re[$charset], $str, $match);
        $slice = join("",array_slice($match[0], $start, $length));
    }
    $fix='';
    if(strlen($slice) < strlen($str)){
        $fix='...';
    }
    return $suffix ? $slice.$fix : $slice;
}


/**
 * 生成订单号
 *
 * @return string
 */
function generate_order_sn($prefix = ''){
    if( $prefix === '' ) $prefix = C('ORDER_SN_PREFIX') ? C('ORDER_SN_PREFIX') : 'HS';
    $time = date('YmdHis', time());
    $rand = rand(10000000, 99999999);
    $order_sn = $prefix . $time . $rand;
    return $order_sn;
}

// 将小数转换为百分数
function percent($float){
    $float = floatval($float) * 100;
    return $float . '%';
}
